package rudia.org.core.spring;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

import rudia.org.core.RuMap;

public class RudiaReqArgumentResolver implements WebArgumentResolver {

	private Log log = LogFactory.getLog(this.getClass());

	/**
	 * 
	 * @see org.springframework.web.bind.support.WebArgumentResolver#resolveArgument(org.springframework.core.MethodParameter,
	 *      org.springframework.web.context.request.NativeWebRequest)
	 * @param methodParameter
	 * @param webRequest
	 * @return
	 * @throws Exception
	 */
	public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {

		Class<?> clazz = methodParameter.getParameterType();

		String paramName = methodParameter.getParameterName();
		if (clazz.equals(RuMap.class) && paramName.equals("rumap")) {

			return ((HttpServletRequest) webRequest.getNativeRequest()).getAttribute("rumap");

		}// end HttpServletRequest if

		if (clazz.equals(RuMap.class) && paramName.equals("loginUser")) {
			RuMap loginUser = (RuMap) webRequest.getAttribute("loginUser", 1);
			if (loginUser != null && loginUser.get("user_id") != null && !loginUser.get("user_id").equals("")) {
				return loginUser;
			} else {
				return null;

			}
		}// end HttpServletRequest if

		return UNRESOLVED;
	}
}